const api = require("../../../../utils/cloudAPIUtils");
const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        id:null,
        info:{},
        imgLoadingList:[],
        tabCur:0,
        tabList:['产品说明','租赁说明','常见问题'],
        modalName:null,
        myLatitude:'',
        myLongitude:'',
        placeListData:[]
    },

    _loadInfo:function(){
        if(this.data.id){
            api.post(api.url.bxscnBatteryInfo,{
                batteryType:this.data.id,
            },res=>{
                if(res.data.code == 200){
                    this.setData({
                        info:res.data.body,
                        imgLoadingList:res.data.body.images
                    })
                }else if(res.data.msg){
                    wx.showModal({
                    content: res.data.msg,
                    showCancel:false,
                    complete: (res) => {
                        wx.navigateBack();
                    }
                    })
                }else {
                    wx.showToast({
                    title: '获取数据失败',
                    icon:'error'
                    })
                    setTimeout(() => {
                        wx.navigateBack();
                    }, 1000);
                }
            })
        }
    },

    _loadPlaceList(){
        let batteryType = this.data.info.id
        wx.showLoading({
            title:'加载中...'
        })
        const _self = this;
        api.post(api.url.bxscnBatteryShop,{
            batteryType:batteryType,
            longitude:this.data.myLongitude,
            latitude:this.data.myLatitude
        },res=>{
            wx.hideLoading();
            if(res.data.code==200){
                _self.setData({
                    placeListData:res.data.body.items,
                })
            }else if(res.data.code==202){
                wx.showModal({
                    content: '暂无可预约租借的网点',
                    showCancel:false,
                    complete: (res) => {
                      _self.setData({
                          modalName:null
                      })
                    }
                })
            }else if(res.data.msg){
                wx.showModal({
                    content: res.data.msg,
                    showCancel:false,
                    complete: (res) => {
                        _self.setData({
                            modalName:null
                        })
                    }
                  })
            }else{
                _self.setData({
                    modalName:null
                })
                wx.showToast({
                  title: '获取数据失败',
                  icon:'error'
                })
            }
        })
    },

    bindHideModal:function(e){
        this.setData({
            modalName:null
        })
    },

    bindImgLoad:function(e){
        let index = e.currentTarget.dataset.index;
        let imgLoadingList = this.data.imgLoadingList;
        imgLoadingList[index] = 100;
        this.setData({imgLoadingList});
    },

    bindTabChange:function(e){
        let index = e.currentTarget.dataset.index;
        this.setData({
            tabCur:index
        })
    },

    btnPlaceSelect:function(e){
        this.setData({
            modalName:'placeSelect'
        })
        this._loadPlaceList();
    },

    toPlaceLease:function(e){
        this.setData({
            modalName:null
        })
        let index = e.currentTarget.dataset.index;
        let place = this.data.placeListData[index];
        const _info = this.data.info;
        wx.navigateTo({
            url: './lease/index',
            events: {
                fromData:function(data){}
            },
            success: function(res) {
                res.eventChannel.emit('fromData',{
                    info:_info,
                    place:place
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            id:options.id,
            myLatitude:options.latitude,
            myLongitude:options.longitude
        })
        this._loadInfo();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})